#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System;
using Pandora;
using UnityEditor;
using UnityEngine;

namespace PandoraEditor.Window
{
    public class CheckTexture : NavFuncBase<UnityEngine.Object>
    {
        public CheckTexture()
        {
            AddNavAction("压缩格式检测", CheckCompress);
            AddNavAction("MipMap检测", CheckMipMap);
            AddNavAction("Read/Write检测", CheckRead);
            AddNavAction("线性采样检测", CheckFiterMode);
            AddNavAction("一键全部", CheckCompress, CheckMipMap, CheckRead);

            AddFixAction("关闭MipMap", FixMipMap);
            AddFixAction("关闭Read/Write", FixRead);
            AddFixAction("关闭AlphaSource", FixAlphaSource);
            AddFixAction("修复全部", FixMipMap, FixRead, FixAlphaSource);

            AddClearButton();
        }

        #region Navigation

        private void CheckCompress(AssetObject assetObject)
        {
            Texture tex = assetObject.texture;
            TextureImporter importer = assetObject.textureImporter;
            if (PdrFileUtil.IncludePath(assetObject.path, new[] { "editor", "gizmos", "plugins" }))
            {
                m_ResultSet.Add("压缩格式-Andorid", "Editor/Plugins/Gizmos", tex);
                return;
            }
            if (!string.IsNullOrEmpty(importer.spritePackingTag))
            {
                m_ResultSet.Add("压缩格式-Andorid", "SpritePacker", tex);
                return;
            }
            TextureFormat format;
            ColorSpace cs;
            int quality;
            importer.ReadTextureImportInstructions(BuildTarget.Android, out format, out cs, out quality);
            if (format != TextureFormat.ETC_RGB4)
            {
                m_ResultSet.Add("压缩格式-Andorid", format, tex);
                LogAppend("压缩格式-Andorid" + format + tex);
            }
            importer.ReadTextureImportInstructions(BuildTarget.iOS, out format, out cs, out quality);
            if (format != TextureFormat.PVRTC_RGB4)
            {
                m_ResultSet.Add("压缩格式-IOS", format, tex);
                LogAppend("压缩格式-IOS" + format + tex);
            }
        }

        private void CheckMipMap(AssetObject assetObject)
        {
            Texture tex = assetObject.texture;
            TextureImporter importer = assetObject.textureImporter;
            if (importer.mipmapEnabled)
            {
                m_ResultSet.Add("MipMap检测", "开启MipMap项", tex);
            }
        }

        private void CheckRead(AssetObject assetObject)
        {
            Texture tex = assetObject.texture;
            TextureImporter importer = assetObject.textureImporter;
            if (importer.isReadable)
            {
                m_ResultSet.Add("Read/Write检测", "开启Read项", tex);
            }
        }

        private void CheckFiterMode(AssetObject assetObject)
        {
            Texture tex = assetObject.texture;
            TextureImporter importer = assetObject.textureImporter;
            if ((int)importer.filterMode == -1)
            {
                m_ResultSet.Add("线性采样检测", "未设置", tex);
            }
            else if (importer.filterMode != FilterMode.Bilinear)
            {
                m_ResultSet.Add("线性采样检测", importer.filterMode, tex);
            }
        }

        #endregion

        #region Repair

        private static void FixMipMap(AssetObject assetObject)
        {
            TextureImporter importer = assetObject.textureImporter;
            if (importer.mipmapEnabled)
            {
                importer.mipmapEnabled = false;
                importer.SaveAndReimport();
            }
        }

        private static void FixRead(AssetObject assetObject)
        {
            TextureImporter importer = assetObject.textureImporter;
            if (importer.isReadable)
            {
                importer.isReadable = false;
                importer.SaveAndReimport();
            }
        }

        private static void FixAlphaSource(AssetObject assetObject)
        {
            Texture tex = assetObject.texture;
            TextureImporter importer = assetObject.textureImporter;
            TextureFormat format;
            ColorSpace cs;
            int quality;
            importer.ReadTextureImportInstructions(BuildTarget.Android, out format, out cs, out quality);
            if (format == TextureFormat.ETC2_RGBA8)
            {
                {
                    importer.isReadable = true;
                    importer.SaveAndReimport();
                }
                Texture2D t2d = tex as Texture2D;
                bool needFixed = true;
                for (int i = 0; i < t2d.width; i++)
                {
                    for (int j = 0; j < t2d.height; j++)
                    {
                        if (t2d.GetPixel(i, j).a < 1)
                        {
                            needFixed = false;
                            break;
                        }
                    }
                    if (!needFixed)
                    {
                        break;
                    }
                }
                if (needFixed)
                {
                    //                        importer.alphaSource = TextureImporterAlphaSource.None;
                }
                if (importer.isReadable)
                {
                    importer.isReadable = false;
                }
                importer.SaveAndReimport();
            }
        }

        #endregion

        private void AddNavAction(string title, params Action<AssetObject>[] handlers)
        {
            base.AddNavButton(title, () =>
            {
                CheckTextureHandler(handlers);
            });
        }
        private void AddFixAction(string title, params Action<AssetObject>[] handlers)
        {
            base.AddFixButton(title, () =>
            {
                CheckTextureHandler(handlers);
            });
        }

        private void CheckTextureHandler(params Action<AssetObject>[] handlers)
        {
            Clear();
            AssetDatabase.Refresh();
            string title = "Check Texture";
            PdrWindowHelper.CheckAllTexture(title, handlers);
            AssetDatabase.Refresh();
            WriteLog();
        }
    }
}